package com.hs.rac.form;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.teamcenter.rac.aif.kernel.AIFComponentEvent;
import com.teamcenter.rac.aif.kernel.AIFInputEvent;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponentEventListener;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentListOfValues;
import com.teamcenter.rac.kernel.TCComponentType;
import com.teamcenter.rac.kernel.TCDateFormat;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCProperty;
import com.teamcenter.rac.kernel.TCPropertyDescriptor;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.stylesheet.InterfaceBufferedPropertyComponent;
import com.teamcenter.rac.stylesheet.InterfacePropertyComponent;
import com.teamcenter.rac.util.Registry;
import com.teamcenter.rac.util.log.Debug;


public class TopBPropertyLOVUIComponent extends CustomLOVUIComponent
    implements InterfacePropertyComponent, InterfaceBufferedPropertyComponent, InterfaceAIFComponentEventListener
{

    public TopBPropertyLOVUIComponent()
    {
        property = null;
        modifiable = true;      
        lovName = null;
        setOpaque(true);
        property = new String();
        lovName = new String();
    }

    public void setProperty(String s)
    {
        property = s;
    }

    public String getProperty()
    {
        return property;
    }

    public void setLovName(String s)
    {
        lovName = s;
    }

    public String getLovName()
    {
        return lovName;
    }

    public void setModifiable(boolean flag)
    {
        modifiable = flag;
        setEnabled(modifiable);
    }

    public boolean getModifiable()
    {
        return modifiable;
    }

    public void setVisible(boolean flag)
    {
        super.setVisible(flag);
    }

   
	public Object getEditableValue()
    {
        return getValue();
    }

    public void setUIFValue(Object obj)
    {
        if(obj != null)
            setText(obj.toString());
        else
            setText("");
    }

    public void load(TCComponentType tccomponenttype)
        throws Exception
    {
        if(tccomponenttype != null)
        {
            TCPropertyDescriptor tcpropertydescriptor = tccomponenttype.getPropertyDescriptor(property);
            load(tcpropertydescriptor);
        }
    }

    public void load(TCPropertyDescriptor tcpropertydescriptor)
        throws Exception
    {
        descriptor = tcpropertydescriptor;
        setEnabled(tcpropertydescriptor.isEnabled() && modifiable);
        //setEnabled( modifiable);
        if(tcpropertydescriptor.isRequired())
        {
            setMandatory(true);
            setRequired(true);
        }
        if(!tcpropertydescriptor.isDisplayable())
            setVisible(false);
    }

    public void load(TCComponent tccomponent)
        throws Exception
    {
        if(property != null)
        {
            TCProperty tcproperty = tccomponent.getTCProperty(property);
            load(tcproperty);
        }
    }

    public void load(TCProperty tcproperty)
        throws Exception
    {
        TCComponent tccomponent = tcproperty.getTCComponent();
        property = tcproperty.getPropertyName();
        if(property != null)
        {
            if(descriptor == null)
            {
                TCPropertyDescriptor tcpropertydescriptor = tcproperty.getPropertyDescriptor();
                load(tcpropertydescriptor);
            }
            component = tccomponent;
            tccomponent.getSession().addAIFComponentEventListener(this);
            TCSession tcsession = tccomponent.getSession();
            TCComponentListOfValues tccomponentlistofvalues = descriptor.getLOV();
            if(tccomponentlistofvalues != null)
            {
                TCProperty atcproperty[] = tccomponentlistofvalues.getInterdependentProperties(tcproperty);
                setDependencyProps(atcproperty);
                setAttachedProp(tcproperty);
                setLovComponent(tccomponentlistofvalues, null);
                
                setEnabled(descriptor.isEnabled() && modifiable);
                //setEnabled(modifiable);
            }
            if(isEnabled() && !tcproperty.isEnabled())
                setEnabled(false);
            if(Debug.isOn("stylesheet,form,property,properties"))
                Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:load propName=").append(property).append(" propValue=").append(tcproperty.toString()).append(" lovName = ").append(lovName).append(" getLOV()=").append(descriptor.getLOV()).append(" isNull=").append(tcproperty.getNullVerdict()).toString());
            if(tccomponentlistofvalues == null && tcsession != null && lovName != null)
                setLovComponent(tcsession, lovName);
            if(tcproperty.getNullVerdict())
            {
                setSelectedValue(null);
                if(tccomponentlistofvalues != null && tccomponentlistofvalues.toString().equals("Unit of Measures"))
                    setSelectedValue(tcsession.getTextService().getTextValue("k_uom_each"));
                return;
            }
            Object obj = null;
            switch(tcproperty.getPropertyType())
            {
            default:
                break;

            case 1: // '\001'
                char c = tcproperty.getCharValue();
                obj = new Character(c);
                break;

            case 2: // '\002'
                Date date = tcproperty.getDateValue();
                TCDateFormat tcdateformat = new TCDateFormat(tccomponent.getSession());
                java.text.SimpleDateFormat simpledateformat = tcdateformat.askDefaultDateFormat();
                if(date != null)
                    obj = simpledateformat.format(date);
                break;

            case 3: // '\003'
                double d = tcproperty.getDoubleValue();
                obj = new Double(d);
                break;

            case 4: // '\004'
                float f = tcproperty.getFloatValue();
                obj = new Double(f);
                break;

            case 5: // '\005'
                int i = tcproperty.getIntValue();
                obj = new Integer(i);
                break;

            case 6: // '\006'
                boolean flag = tcproperty.getLogicalValue();
                obj = Boolean.valueOf(flag);
                break;

            case 7: // '\007'
                short word0 = tcproperty.getShortValue();
                obj = new Integer(word0);
                break;

            case 8: // '\b'
                obj = tcproperty.getStringValue();
            
                break;

            case 12: // '\f'
                obj = tcproperty.getNoteValue();
                break;

            case 0: // '\0'
            case 9: // '\t'
            case 10: // '\n'
            case 11: // '\013'
            case 13: // '\r'
                obj = tcproperty.getReferenceValue();
                break;
            }
            setSelectedValue(obj);
            //setSelectedValue("");
        }
    }

    public void save(TCComponent tccomponent)
        throws Exception
    {
        TCProperty tcproperty = getPropertyToSave(tccomponent);
        if(savable)
            tccomponent.setTCProperty(tcproperty);
    }

    public void save(TCProperty tcproperty)
        throws Exception
    {
        TCProperty tcproperty1 = getPropertyToSave(tcproperty);
        if(savable && tcproperty1 != null)
            tcproperty1.getTCComponent().setTCProperty(tcproperty);
    }

    public TCProperty saveProperty(TCComponent tccomponent)
        throws Exception
    {
        TCProperty tcproperty = getPropertyToSave(tccomponent);
        if(savable)
            return tcproperty;
        else
            return null;
    }

    public TCProperty saveProperty(TCProperty tcproperty)
        throws Exception
    {
        TCProperty tcproperty1 = getPropertyToSave(tcproperty);
        if(savable)
            return tcproperty1;
        else
            return null;
    }

    public TCProperty getPropertyToSave(TCComponent tccomponent)
        throws Exception
    {
        if(property != null)
        {
            TCProperty tcproperty = tccomponent.getTCProperty(property);
            return getPropertyToSave(tcproperty);
        } else
        {
            savable = false;
            return null;
        }
    }

    public TCProperty getPropertyToSave(TCProperty tcproperty)
        throws Exception
    {
        savable = false;
        if(tcproperty != null)
        {
            if(!tcproperty.isEnabled() || !modifiable)
            {
                if(Debug.isOn("stylesheet,form,property,properties"))
                    Debug.println((new StringBuilder()).append("PropertyLOVUIComponent: save propName=").append(property).append(" not modifiable, skip.").toString());
                return null;
            }
            TCComponentListOfValues tccomponentlistofvalues = getLovComponent();
            if(tccomponentlistofvalues == null)
            {
                if(Debug.isOn("stylesheet,form,property,properties"))
                    Debug.println((new StringBuilder()).append("PropertyLOVUIComponent: save propName=").append(property).append(" not lov found, skip.").toString());
                return null;
            }
            Object obj = getValue();
            String s = getText();
            boolean flag = tcproperty.getNullVerdict();
            short word0 = tccomponentlistofvalues.getListOfValuesType();
            if(Debug.isOn("stylesheet,form,property,properties"))
                Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(" oldValue=").append(tcproperty.toString()).append(" lovType=").append(word0).append(" propType = ").append(tcproperty.getPropertyType()).toString());
            Registry registry = Registry.getRegistry("com.teamcenter.rac.stylesheet.stylesheet");
            String s1 = (new StringBuilder()).append(getProperty()).append(" ").append(registry.getString("cannotSaveUserInput")).toString();
            TCException tcexception = new TCException(s1);
            tcexception.errorSeverities = (new int[] {
                2
            });
            try
            {
                switch(tcproperty.getPropertyType())
                {
                default:
                    break;

                case 1: // '\001'
                    if(s.length() > 0)
                    {
                        char c = s.charAt(0);
                        if(c != tcproperty.getCharValue())
                        {
                            tcproperty.setCharValueData(c);
                            savable = true;
                            if(Debug.isOn("stylesheet,form,property,properties"))
                                Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(",set char = ").append(c).toString());
                            break;
                        }
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip").toString());
                        break;
                    }
                    if(flag)
                        break;
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent: save propName=").append(property).append(", set to null.").toString());
                    tcproperty.setNullVerdict(true);
                    savable = true;
                    break;

                case 2: // '\002'
                    Date date = null;
                    if(obj != null && (obj instanceof Date))
                        date = (Date)obj;
                    else
                    if(s != null && s.length() > 0)
                    {
                        TCDateFormat tcdateformat = new TCDateFormat(tcproperty.getTCComponent().getSession());
                        java.text.SimpleDateFormat simpledateformat = tcdateformat.askDefaultDateFormat();
                        date = simpledateformat.parse(s);
                    } else
                    if(!flag)
                    {
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent: save propName=").append(property).append(", set to null.").toString());
                        tcproperty.setNullVerdict(true);
                        savable = true;
                    }
                    if(date != null && !date.equals(tcproperty.getDateValue()))
                    {
                        tcproperty.setDateValueData(date);
                        savable = true;
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(",set date = ").append(date).toString());
                        break;
                    }
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip; or set to null.").toString());
                    break;

                case 3: // '\003'
                    Double double1 = null;
                    if(obj instanceof Double)
                        double1 = (Double)obj;
                    else
                    if(s != null && s.length() > 0)
                        double1 = Double.valueOf(s);
                    else
                    if(!flag)
                    {
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent: save propName=").append(property).append(", set to null.").toString());
                        tcproperty.setNullVerdict(true);
                        savable = true;
                    }
                    if(double1 != null && double1.doubleValue() != tcproperty.getDoubleValue())
                    {
                        tcproperty.setDoubleValueData(double1.doubleValue());
                        savable = true;
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(",set double = ").append(double1).toString());
                        break;
                    }
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip; or set to null.").toString());
                    break;

                case 4: // '\004'
                    Float float1 = null;
                    if(obj instanceof Float)
                        float1 = (Float)obj;
                    else
                    if(s != null && s.length() > 0)
                        float1 = Float.valueOf(s);
                    else
                    if(!flag)
                    {
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent: save propName=").append(property).append(", set to null.").toString());
                        tcproperty.setNullVerdict(true);
                        savable = true;
                    }
                    if(float1 != null && float1.floatValue() != tcproperty.getFloatValue())
                    {
                        tcproperty.setFloatValueData(float1.floatValue());
                        savable = true;
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(",set float = ").append(float1).toString());
                        break;
                    }
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip; or set to null.").toString());
                    break;

                case 5: // '\005'
                    Integer integer = null;
                    if(obj instanceof Integer)
                        integer = (Integer)obj;
                    else
                    if(s != null && s.length() > 0)
                        integer = Integer.valueOf(s);
                    else
                    if(!flag)
                    {
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent: save propName=").append(property).append(", set to null.").toString());
                        tcproperty.setNullVerdict(true);
                        savable = true;
                    }
                    if(integer != null && integer.intValue() != tcproperty.getIntValue())
                    {
                        tcproperty.setIntValueData(integer.intValue());
                        savable = true;
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(",set int = ").append(integer).toString());
                        break;
                    }
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip; or set to null.").toString());
                    break;

                case 6: // '\006'
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", doesn't apply to logical type").toString());
                    break;

                case 7: // '\007'
                    Short short1 = null;
                    if(obj instanceof Short)
                        short1 = (Short)obj;
                    else
                    if(s != null && s.length() > 0)
                        short1 = Short.valueOf(s);
                    else
                    if(!flag)
                    {
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent: save propName=").append(property).append(", set to null.").toString());
                        tcproperty.setNullVerdict(true);
                        savable = true;
                    }
                    if(short1 != null && short1.shortValue() != tcproperty.getShortValue())
                    {
                        tcproperty.setShortValueData(short1.shortValue());
                        savable = true;
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(",set short = ").append(short1).toString());
                        break;
                    }
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip; or set to null.").toString());
                    break;

                case 8: // '\b'
                    if(!s.equals(tcproperty.getStringValue()))
                    {
                        tcproperty.setStringValueData(s);
                        savable = true;
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(",set str = ").append(getText()).toString());
                        break;
                    }
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip").toString());
                    break;

                case 12: // '\f'
                    if(!s.equals(tcproperty.getNoteValue()))
                    {
                        tcproperty.setNoteValueData(s);
                        savable = true;
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(",set note = ").append(getText()).toString());
                        break;
                    }
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip").toString());
                    break;

                case 0: // '\0'
                case 9: // '\t'
                case 10: // '\n'
                case 11: // '\013'
                case 13: // '\r'
                case 14: // '\016'
                    TCComponent tccomponent = tcproperty.getReferenceValue();
                    if(obj != null && (obj instanceof TCComponent))
                    {
                        if(!obj.equals(tccomponent))
                        {
                            tcproperty.setReferenceValueData((TCComponent)obj);
                            savable = true;
                            if(Debug.isOn("stylesheet,form,property,properties"))
                                Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(",set ref = ").append(obj).toString());
                            break;
                        }
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip").toString());
                        break;
                    }
                    if(tccomponent != null && s.equals(tccomponent.toString()) || tccomponent == null && s.equals(""))
                    {
                        if(Debug.isOn("stylesheet,form,property,properties"))
                            Debug.println((new StringBuilder()).append("PropertyLOVUIComponent:save propName=").append(property).append(", value not changed, skip").toString());
                        break;
                    }
                    if(flag)
                        break;
                    if(Debug.isOn("stylesheet,form,property,properties"))
                        Debug.println((new StringBuilder()).append("PropertyLOVUIComponent: save propName=").append(property).append(", set to null.").toString());
                    tcproperty.setNullVerdict(true);
                    savable = true;
                    break;
                }
            }
            catch(NumberFormatException numberformatexception)
            {
                if(Debug.isOn("stylesheet,form,property,properties"))
                    Debug.printStackTrace(numberformatexception);
                throw tcexception;
            }
        }
        return tcproperty;
    }

    public boolean isPropertyModified(TCComponent tccomponent)
        throws Exception
    {
        if(property != null)
        {
            TCProperty tcproperty = tccomponent.getTCProperty(property);
            return isPropertyModified(tcproperty);
        } else
        {
            return false;
        }
    }

    public boolean isPropertyModified(TCProperty tcproperty)
        throws Exception
    {
        savable = false;
        if(!tcproperty.isEnabled() || !modifiable)
            return false;
        TCComponentListOfValues tccomponentlistofvalues = getLovComponent();
        if(tccomponentlistofvalues == null)
            return false;
        Object obj = getValue();
        String s = getText();
        boolean flag = tcproperty.getNullVerdict();
        switch(tcproperty.getPropertyType())
        {
        case 6: // '\006'
        default:
            break;

        case 1: // '\001'
            if(s.length() > 0)
            {
                char c = s.charAt(0);
                if(c != tcproperty.getCharValue())
                    savable = true;
                break;
            }
            if(!flag)
                savable = true;
            break;

        case 2: // '\002'
            Object obj1 = null;
            Date date2 = tcproperty.getDateValue();
            if(obj != null && (obj instanceof Date))
            {
                Date date = (Date)obj;
                if(!date.equals(date2))
                    savable = true;
                break;
            }
            if(s != null && s.length() > 0)
            {
                TCDateFormat tcdateformat = new TCDateFormat(tcproperty.getTCComponent().getSession());
                java.text.SimpleDateFormat simpledateformat = tcdateformat.askDefaultDateFormat();
                Date date1 = simpledateformat.parse(s);
                String s1 = simpledateformat.format(date2);
                if(!s.equals(s1))
                    savable = true;
                break;
            }
            if(!flag)
                savable = true;
            break;

        case 3: // '\003'
            Double double1 = null;
            if(obj instanceof Double)
                double1 = (Double)obj;
            else
            if(s != null && s.length() > 0)
                double1 = Double.valueOf(s);
            else
            if(!flag)
                savable = true;
            if(double1 != null && double1.doubleValue() != tcproperty.getDoubleValue())
                savable = true;
            break;

        case 4: // '\004'
            Float float1 = null;
            if(obj instanceof Float)
                float1 = (Float)obj;
            else
            if(s != null && s.length() > 0)
                float1 = Float.valueOf(s);
            else
            if(!flag)
                savable = true;
            if(float1 != null && float1.floatValue() != tcproperty.getFloatValue())
                savable = true;
            break;

        case 5: // '\005'
            Integer integer = null;
            if(obj instanceof Integer)
                integer = (Integer)obj;
            else
            if(s != null && s.length() > 0)
                integer = Integer.valueOf(s);
            else
            if(!flag)
                savable = true;
            if(integer != null && integer.intValue() != tcproperty.getIntValue())
                savable = true;
            break;

        case 7: // '\007'
            Short short1 = null;
            if(obj instanceof Short)
                short1 = (Short)obj;
            else
            if(s != null && s.length() > 0)
                short1 = Short.valueOf(s);
            else
            if(!flag)
                savable = true;
            if(short1 != null && short1.shortValue() != tcproperty.getShortValue())
                savable = true;
            break;

        case 8: // '\b'
            if(!s.equals(tcproperty.getStringValue()))
                savable = true;
            break;

        case 12: // '\f'
            if(!s.equals(tcproperty.getNoteValue()))
                savable = true;
            break;

        case 0: // '\0'
        case 9: // '\t'
        case 10: // '\n'
        case 11: // '\013'
        case 13: // '\r'
        case 14: // '\016'
            TCComponent tccomponent = tcproperty.getReferenceValue();
            if(obj != null && (obj instanceof TCComponent))
            {
                if(!obj.equals(tccomponent))
                    savable = true;
                break;
            }
            if(!s.equals(tccomponent.toString()) && !flag)
                savable = true;
            break;
        }
        return savable;
    }

    public void processComponentEvents(AIFComponentEvent aaifcomponentevent[])
    {
        Object obj = null;
        Arrays.sort(aaifcomponentevent);
        for(int i = 0; i < aaifcomponentevent.length; i++)
        {
            TCComponent tccomponent = (TCComponent)aaifcomponentevent[i].getComponent();
            if(tccomponent != component || !(aaifcomponentevent[i] instanceof AIFInputEvent))
                continue;
            AIFInputEvent aifinputevent = (AIFInputEvent)aaifcomponentevent[i];
            if(!aifinputevent.getProperty().equals(getProperty()))
                continue;
            Object obj1 = aifinputevent.getInputValue();
            if(obj1 != null)
            {
                if(obj1 instanceof List)
                {
                    List list = (List)obj1;
                    if(list.size() > 0)
                        setSelectedValue(list.get(0));
                    else
                        setSelectedValue(null);
                } else
                {
                    setSelectedValue(obj1);
                }
            } else
            {
                setText("");
            }
        }

    }

    protected String property;
    protected boolean modifiable;
    protected String lovName;
    protected TCPropertyDescriptor descriptor;
    protected TCComponent component;
    private boolean savable;

}
